﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.Modules;
using YT.DB;
using YT.Commons;

namespace YT.XH.SuddenDeafness
{
    public partial class frmSDTreatmentEffectAfterTreate : Form
    {
        private ArrayList marrlist = new ArrayList();
        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private FormStatus mfrmStatus = new FormStatus();
        private string mstrSeqNo = string.Empty;
        public frmSDTreatmentEffectAfterTreate()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
            mstrSeqNo = "1";
        }
        public frmSDTreatmentEffectAfterTreate(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mstrSeqNo = "1";
        }

        public frmSDTreatmentEffectAfterTreate(string strSickRecID, FormStatus frmStatus)
        {
            InitializeComponent();           
            mstrSickRecID = strSickRecID;
            mfrmStatus = frmStatus;
        }
        public frmSDTreatmentEffectAfterTreate(string strSickRecID, FormStatus frmStatus, string strExamSeqNo)
        {
            InitializeComponent();
            mstrSeqNo = strExamSeqNo; ;
            mstrSickRecID = strSickRecID;
            mfrmStatus = frmStatus;
        }

        private void frmSDTreatmentEffectAfterTreate_Load(object sender, EventArgs e)
        {
            this.Text = "治疗结束后综合疗效评估";
            FillData();
            timer1.Start();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (UpdateHdr()) this.Close(); 
            }
            else
                this.Close();
        }

        private void FindPanel(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
                {
                    marrlist.Add(con);
                }
                if (con.Controls.Count > 0)
                {
                    FindPanel(con);
                }
            }
        }

        private void FillData()
        {
            string strTableName = "EffectAfterTreateResult";
            DBAccess dbcon = new DBAccess(false, false);


            #region 填写方案名称
            string strCaseName = string.Empty;
            string strThrombolysis = string.Empty;
            string strSQL = "select * from mTreatHistory where SickRecID ='" + mstrSickRecID + "' order by RecID";
            DataTable dtCase = dbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtCase.Rows)
            {
                strCaseName += CvtFunc.ToString(drRow["TreatCaseName"])  + " + "  ;
                strThrombolysis = CvtFunc.ToString(drRow["Thrombolysis"]);
            }
            if (strCaseName.Length   > 4)
            {
                strCaseName = strCaseName.Substring(0, strCaseName.Length - 3);
                txtCaseName.Text = strCaseName;
            }
            if (strThrombolysis == this.radA.Text)
            {
                this.radA.Checked = true; 
            }
            else if (strThrombolysis == this.radB.Text)
            {
                this.radB.Checked = true; 
            }
            #endregion
            foreach (Control cons in this.Controls)
            {
                FindPanel(cons);
            }
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    string strTitleType = TitleType.SuddenDeafness.ToString();
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            strColTitle = conText.Tag.ToString();
                            strSQL = "select top 1 Result from " + strTableName + " where rowno='" + strColTitle + "' and  ExamSeqNo='" + mstrSeqNo + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and ExamSeqNo='"+ mstrSeqNo  +"' and Type='" + TitleType.SuddenDeafness  + "' and SickRecID='"+ mstrSickRecID  +"'";
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null) continue;
                            strResult = CvtFunc.ToString(drCheck["Result"]);
                            if (conText.GetType().Name == "TextBox" )
                            {
                                conText.Text = strResult;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                if (strResult != "") ((DateTimePicker)conText).Value = CvtFunc.ToDateTime(strResult); 
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                if (strResult == "false")
                                    ((RadioButton)conText).Checked = false;
                                else if (strResult == "true")
                                    ((RadioButton)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                if (strResult == "false")
                                    ((CheckBox)conText).Checked = false;
                                else if (strResult == "true")
                                    ((CheckBox)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                continue;
                            }
                        }
                    }

                }
            }
            dbcon.CloseCon();
        }

        private bool UpdateData(string strExamSeqNo)
        {
            string strTableName = "EffectAfterTreateResult";
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    DataRow drRow = null;
                    string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type,ExamSeqNo";
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            if (conText.GetType().Name == "TextBox" )
                            {
                                strResult = conText.Text;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                strResult =BaseFunctions.FormatDate (  ((DateTimePicker)conText).Value.ToString () , DateFormatOption.DateFmtYMD) ;
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                strResult = (((RadioButton)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                strResult = (((CheckBox)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                strResult = "";
                                continue;
                            }
                            strColTitle = conText.Tag.ToString();

                            string strSQL = "select top 1 * from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and ExamSeqNo='"+ mstrSeqNo  +"' and SickRecID='"+ mstrSickRecID  +"' and Type='" + TitleType.SuddenDeafness + "'";
                            bool blnIsNew = false;
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null)
                            {
                                blnIsNew = true;
                                drRow = dbcon.GetDataTable("select top 0 * from " + strTableName + " ").NewRow();
                            }
                            else
                            {
                                blnIsNew = false;
                                drRow = dbcon.GetDataFirstRow("select top 1 * from " + strTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                            }

                            drRow["SickRecID"] = strSickRecID;
                            drRow["HdrSeqno"] = strHdrSeqNo;
                            drRow["DtlSeqNo"] = strDtlSeqNo;
                            drRow["RowNo"] = strColTitle;
                            drRow["Result"] = strResult;
                            drRow["Type"] = TitleType.SuddenDeafness ;
                            drRow["ExamSeqNo"] = strExamSeqNo;
                            if (blnIsNew)
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.AddNew);
                            else
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.Update);
                        }
                    }
                }
            }
            dbcon.CloseCon();
            return true;
        }

        private bool UpdateHdr()
        {
            string strSeqNo = string.Empty;
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = string.Empty;

            strSQL = "select * from EffectAfterTreateHdr where SickRecID='" + mstrSickRecID + "' and Type='" + TitleType.SuddenDeafness + "' and SeqNo='1'  ";
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow == null)
                mfrmStatus = FormStatus.AddNew;
            else
                mfrmStatus = FormStatus.Update;

            if (mfrmStatus == FormStatus.AddNew)
            {
                //strSeqNo = DataFunctions.GetFieldValue("TreatmentEffectHdr", "max(SeqNo) as SeqNo", " Type='" + TitleType.SuddenDeafness + "' and SickRecID='"+ mstrSickRecID +"'");
                //strSeqNo = CvtFunc.ToString(CvtFunc.ToInt(strSeqNo) + 1);
                strSeqNo = "1";
                strSQL = " Insert into EffectAfterTreateHdr(  SickRecID, SeqNo, ExamDate,Type ) " +
                         " values (  '" + mstrSickRecID + "','" + strSeqNo + "','" + BaseFunctions.FormatDate(DateTime.Now.Date.ToString(), DateFormatOption.DateFmtYMD) + "' " +
                         " ,'" + TitleType.SuddenDeafness + "' ) ";
                dbcon.ExecuteSql(strSQL);
                mstrSeqNo = strSeqNo;
            }
            else if (mfrmStatus == FormStatus.Update)
            {
                //strSeqNo = mstrSeqNo;
                strSeqNo = "1";
                mstrSeqNo = "1";
            }
            UpdateData(strSeqNo);
            mfrmStatus = FormStatus.Update;
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
             if (UpdateHdr()) this.Close(); 
        }

        private void btnJacobson_Click(object sender, EventArgs e)
        {
            if (!UpdateHdr()) return;
            frmSDJacobsonTitle frmTitle = new frmSDJacobsonTitle(mstrSickRecID, mstrSeqNo, JobocoType.AfterTreat );
            double dblFscore = 0;
            double dblEscore = 0;
            double dblPscore = 0;
            double dblTotalscore = 0;
            double dblSubScore = 0;
            frmTitle.GetTotalJacobson(ref dblTotalscore, ref dblFscore, ref dblEscore, ref dblPscore, ref dblSubScore);

            txtTotalScore.Text = CvtFunc.ToString(dblTotalscore); 
        }

        private void frmSDTreatmentEffectAfterTreate_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
            frmGifImage frmGIF = new frmGifImage();
            frmGIF.TopMost = true;
            frmGIF.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void btnCalTHI_Click(object sender, EventArgs e)
        {
            if (!UpdateHdr()) return;
            frmSDTHITitle frmTitle = new frmSDTHITitle(mstrSickRecID, mstrSeqNo, JobocoType.AfterTreat);
            double dblFscore = 0;
            double dblEscore = 0;
            double dblPscore = 0;
            double dblTotalscore = 0;
            double dblSubScore = 0;
            frmTitle.GetTotalJacobson(ref dblTotalscore, ref dblFscore, ref dblEscore, ref dblPscore, ref dblSubScore);

            txtTHITotalscore.Text = CvtFunc.ToString(dblTotalscore); 
        }

    }
}
